package com.xbongbong.saas.enums.business;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.ScreenTypeEnum;
import com.xbongbong.paas.enums.SheetTypeEnum;
import com.xbongbong.saas.constant.PaymentConstant;
import com.xbongbong.saas.enums.SetTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author zcp
 * @version v1.0
 * @since v1.0
 */
@Getter
public enum OtherExpenseEnum {
    // other_1：基础信息
    BASE_INFO("other_1", "enum.otherExpense.attrName.base_info", "baseInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 10000, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //text_1：类型，其他收入单、其他支出单的类型字段attr都统一用PaymentConstant.ATTR_SHEET_TYPE_OTHER
    SHEET_TYPE_OTHER(PaymentConstant.ATTR_SHEET_TYPE_OTHER, "enum.otherExpense.attrName.sheet_type_other", "sheetTypeOther", FieldTypeEnum.COMBO.getType(), 1, 0, 0, ShowTypeEnum.LIST_DETAIL.getCode(), 9990, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // serialNo：编号
    SHEET_NO("serialNo", "enum.otherExpense.attrName.sheet_no", "sheetNo", FieldTypeEnum.SERIALNO.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9980, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //num_1：金额
    AMOUNT("num_1", "enum.otherExpense.attrName.amount", "amount", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9970, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //num_2：开票金额 - 其他收入、其他支出对应发票暂未开发，该字段隐藏
    INVOICE_AMOUNT("num_2", "enum.otherExpense.attrName.invoice_amount", "invoiceAmount", FieldTypeEnum.NUM.getType(), 1, 1, 1, ShowTypeEnum.NONE.getCode(), 9960, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //text_2：负责人
    BELONG_ID("text_2", "enum.otherExpense.attrName.belong_id", "belongId", FieldTypeEnum.USER.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9950, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //date_1：付款日期
    EXPENSE_TIME("date_1", "enum.otherExpense.attrName.expense_time", "expenseTime", FieldTypeEnum.DATETIME.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9940, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //text_3：付款账户
    FUND_ACCOUNT("text_3", "enum.otherExpense.attrName.fund_account", "fundAccount", FieldTypeEnum.COMBO.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9930, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //text_4：支付方式
    PAY_METHOD("text_4", "enum.otherExpense.attrName.pay_method", "payMethod", FieldTypeEnum.COMBO.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9920, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),

    //other_2：扩展信息
    SYSTEM_INFO("other_2", "enum.otherExpense.attrName.system_info", "systemInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9910, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //text_5：备注
    MEMO("text_5", "enum.otherExpense.attrName.memo", "memo", FieldTypeEnum.TEXTAREA.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9900, 1, 0, ScreenTypeEnum.SCREEN_SHOW.getType()),
    //file_1：附件
    FILE("file_1", "enum.otherExpense.attrName.file", "file", FieldTypeEnum.UPLOAD.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9890, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //text_6：关联原始支出单
    ORIGIN_ID("text_6", "enum.otherExpense.attrName.origin_id", "originId", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9880, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),

    CREATOR_ID("creatorId", "enum.otherExpense.attrName.creator_id", "creatorId", FieldTypeEnum.CREATORID.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9870, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ADD_TIME("addTime", "enum.otherExpense.attrName.add_time", "addTime", FieldTypeEnum.ADDTIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9860, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    UPDATE_TIME("updateTime", "enum.otherExpense.attrName.update_time", "updateTime", FieldTypeEnum.UPDATETIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9850, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),


    //text_7：关联原始支出单隐藏
    ORIGIN_ID_LINK_TEXT("text_7", "enum.otherExpense.attrName.origin_id_link_text", "originIdLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9800, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),

    ;
    /**
     * 英文名称
     */
    private String attr;
    /**
     * 中文名称
     */
    private String attrName;
    /**
     * 对应saas中的attr
     */
    private String saasAttr;
    /**
     * 字段类型，详见
     *
     * @see FieldTypeEnum
     */
    private Integer fieldType;
    /**
     * 重命名、判重、启用必填等权限
     * 1.重命名+字段权限
     * 2.重命名+字段权限+查重
     * 3.重命名+字段权限+启用、必填
     * 4.重命名+字段权限+查重+启用、必填
     * 注：pro中启用和必填是同一个权限，同时启用或同时关闭
     */
    private Integer setType;
    /**
     * 模板设计阶段字段是否允许编辑
     * 1：允许编辑
     * 0：不允许编辑
     */
    private Integer designEditable;
    /**
     * 字段是否显示在模板设计界面
     * 1：不显示
     * 0：显示
     */
    private Integer editHide;
    /**
     * 字段在各个界面（新建、编辑、列表、详情）的展现形式
     * 详见
     *
     * @see ShowTypeEnum
     */
    private Integer showType;
    /**
     * 字段在模板设计界面的排序
     * 建议不要用连续的数字，可以用等差数列，以防以后在两个字段之间加入新字段
     */
    private Integer sort;
    /**
     * 字段在各个界面是否可见(启用)
     * 1：可见
     * 0：不可见
     * 注：字段在具体页面的可见性需要用属性和showType联合判断，在visible=1的情况下判断showType
     */
    private Integer visible;
    /**
     * 字段是否必填
     * 1：必填
     * 0：不必填
     */
    private Integer required;
    /**
     * 筛选类型
     * 1：可筛选
     * 2：不可筛选
     */
    private Integer screenType;

    OtherExpenseEnum(String attr, String attrName, String saasAttr, Integer fieldType, Integer setType, Integer designEditable,
                     Integer editHide, Integer showType, Integer sort, Integer visible, Integer required, Integer screenType) {
        this.attr = attr;
        this.attrName = attrName;
        this.saasAttr = saasAttr;
        this.fieldType = fieldType;
        this.setType = setType;
        this.designEditable = designEditable;
        this.editHide = editHide;
        this.showType = showType;
        this.sort = sort;
        this.visible = visible;
        this.required = required;
        this.screenType = screenType;
    }

    /**
     * 对data内的字段拼上“data.”
     * @param cache
     * @return java.lang.String
     * @author zcp
     * @date 2019/3/1 18:12
     * @since v1.0
     * @version v1.0
     */
    public static String getAttrConnectData(OtherExpenseEnum cache) {
        return "data." + cache.getAttr();
    }
    /**
     * 获取es查询、聚合字段
     * data在库内是json对象，对data字段内某个key做es【精确查询】时，需要在其前拼上“data.”，且【text等类型的】要在后面拼“.keyword”
     * @param cache 当前枚举对象
     * @return java.lang.String 处理后的聚合字段
     * @author zcp
     * @date 2019/2/21 14:59
     * @since v1.0
     * @version v1.0
     */
    public static String getEsAttr4Keyword(OtherExpenseEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.fieldType);
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getAttr() + ".keyword";
        }
        return "data." + cache.getAttr();
    }

    /**
     * 流程中获取隐藏字段
     * @return
     */
    public static List<OtherExpenseEnum> getDisableField() {
        List<OtherExpenseEnum> paymentSheetEnums = new ArrayList<>();
        for (OtherExpenseEnum paymentSheetEnum : values()) {
            if (Objects.equals(paymentSheetEnum.getShowType(), ShowTypeEnum.NONE.getCode()) || Objects.equals(paymentSheetEnum.getEditHide(), 1)
                    || Objects.equals(paymentSheetEnum.getFieldType(), FieldTypeEnum.CREATORID.getType()) || Objects.equals(paymentSheetEnum.getFieldType(), FieldTypeEnum.ADDTIME.getType())) {
                paymentSheetEnums.add(paymentSheetEnum);
            }
        }
        return  paymentSheetEnums;
    }

    /**
     * 把传入的字段（类型为LINK_BUSINESS_SINGLE）的转为其隐藏字段
     * @param attr 字段名
     * @return java.lang.String
     * @author zcp
     * @date 2019/7/4 15:31
     * @since v1.0
     * @version v1.0
     */
    public static String changeToHide4Link(String attr) {
        if (Objects.equals(attr, ORIGIN_ID.attr)) {
            return ORIGIN_ID_LINK_TEXT.attr;
        }
        return attr;
    }

    /**
     * app端默认标题显示字段
     *
     * @return java.util.List<java.lang.Object>
     * @author 徐俊杰
     * @date 2019/11/20 15:22
     * @since v1.0
     */
    public static List<Object> getTitleAttrList() {
        return Collections.singletonList(SHEET_NO);
    }

    /**
     * app端默认摘要
     *
     * @return java.util.List<java.lang.Object>
     * @author 徐俊杰
     * @date 2019/11/20 15:24
     * @since v1.0
     */
    public static List<Object> getSummaryAttrList() {
        return Arrays.asList(BELONG_ID, EXPENSE_TIME, FUND_ACCOUNT);
    }

    /**
     * 得到所有的字段attr
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @date 19/11/26 026 10:25
     * @update 19/11/26 026 10:25
     * @since v1.0
     * @version v1.0
     */
    public static List<String> getAllAttr() {
        List<String> allAttr = new ArrayList<>();
        for (OtherExpenseEnum cache : values()) {
            allAttr.add(cache.attr);
        }
        return allAttr;
    }

    /**
     * 得到新建or编辑时各种付款单禁止修改的字段
     * 具体见文档：https://xbb.yuque.com/lfwuxq/vsf9sv/pmq6pf#XhfVx
     * @param addUnableEdit true 新增是禁止编辑，false 编辑时各类型付款单不能编辑的字段
     * @param sheetTypeEnum 类型
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public static List<String> getUnableEditAttrList(boolean addUnableEdit, SheetTypeEnum sheetTypeEnum) {
        if (addUnableEdit) {
            //新增时各类型其他支出单不能编辑的字段
            switch (sheetTypeEnum) {
                case RED_OTHER_EXPENSE:
                    return Arrays.asList(ORIGIN_ID.attr, ORIGIN_ID_LINK_TEXT.attr);
                default:
                    return new ArrayList<>();
            }
        } else {
            List<String> unableEditAttrList = new ArrayList<>();
            unableEditAttrList.add(SHEET_NO.attr);
            unableEditAttrList.add(AMOUNT.attr);
            unableEditAttrList.add(SHEET_TYPE_OTHER.attr);
            unableEditAttrList.add(INVOICE_AMOUNT.attr);
            unableEditAttrList.add(BELONG_ID.attr);
            unableEditAttrList.add(EXPENSE_TIME.attr);
            unableEditAttrList.add(FUND_ACCOUNT.attr);
            //再加上红冲的，关联原始单据
            unableEditAttrList.add(ORIGIN_ID.attr);
            unableEditAttrList.add(ORIGIN_ID_LINK_TEXT.attr);
            return unableEditAttrList;
        }
    }
    /**
     * 审批流程内不可编辑，必可见字段
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public static List<String> processNoneEditMustShow(){
        return Arrays.asList(AMOUNT.attr, SHEET_TYPE_OTHER.attr, INVOICE_AMOUNT.attr, BELONG_ID.attr, EXPENSE_TIME.attr, FUND_ACCOUNT.attr);
    }

    public String getAttrName() {
        return I18nMessageUtil.getMessage(attrName);
    }

    public String getI18nAttrName() {
        return attrName;
    }

    public static String getI18nValue(String value) {
        return I18nMessageUtil.getMessage(value);
    }
}
